package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://club.china.com/ 中华网社区
 * @author allen
 */
public class ChinaBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://club.china.com/data/thread/.*/.*/.*/.*/.*_1.html",
				"http://.*.shanzhai.china.com/club/thread/.*/.*/.*/.*/.*_1.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);

			title =doc.select("title").text();
			if(title==null || title.trim().length()==0){
			}
			if(title.indexOf("_")!=-1){
				title=title.substring(0,title.indexOf("_"));
			}

			content = HtmlCleaner.getContentHtml(url, doc.select("div.topic div.postContent"));
			if(content==null || content.trim().length()==0){
				//content=doc.select("html body center div.wrapper div#mainDiv div.body form#form1 div.article_box div#content0.content").text();
			}
			String tempdate=doc.select("div.topic div.postInfo div.authorInfo ul li.time").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("div.imgTopic div.imgTopicCon div.imgTopicTit h4 span").text();
			}
			date = DateUtils.matchDate(tempdate);


			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);

			String clickComentstr=doc.select("div.ctrlBox div.topPagesTwo form table tbody tr td").text();
			if(clickComentstr!=null && clickComentstr.trim().length()!=0){
				if(clickComentstr.indexOf("阅读:")!=-1){
					String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读:")+"阅读:".length());
					if(clickNumstr.indexOf(" ")!=-1){
						clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
				if(clickComentstr.indexOf("参与:")!=-1){
					String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("参与:")+"参与:".length());
					if(commentNumstr.indexOf(" ")!=-1){
						commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
					try {
						commentNum=Integer.parseInt(commentNumstr);
					} catch (Exception e) {
						// TODO: handle exception
					}
					}
				}
			}else{
				clickComentstr=doc.select("div.imgTopic div.imgTopicCon div.imgTopicTit h4 span").text();
				if(clickComentstr!=null && clickComentstr.trim().length()!=0){
					if(clickComentstr.indexOf("阅读:")!=-1){
						String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读:")+"阅读:".length());
						if(clickNumstr.indexOf(" ")!=-1){
							clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
							try {
								clickNum=Integer.parseInt(clickNumstr);
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
					}
					if(clickComentstr.indexOf("参与:")!=-1){
						String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("参与:")+"参与:".length());
						if(commentNumstr.indexOf(" ")!=-1){
							commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
						try {
							commentNum=Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
						}
					}
				}else{
					clickComentstr=doc.select("div#container div.mainBodyLayout div.forumBody div.forumMain div#thread_mainBody div.ctrlBox div.adminBtn").text();
					if(clickComentstr!=null && clickComentstr.trim().length()!=0){
						if(clickComentstr.indexOf("阅读（")!=-1){
							String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读（")+"阅读（".length());
							if(clickNumstr.indexOf("）")!=-1){
								clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf("）"));
								try {
									clickNum=Integer.parseInt(clickNumstr);
								} catch (Exception e) {
									// TODO: handle exception
								}
							}
						}
						if(clickComentstr.indexOf("回复（")!=-1){
							String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复（")+"回复（".length());
							if(commentNumstr.indexOf("）")!=-1){
								commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf("）"));
							try {
								commentNum=Integer.parseInt(commentNumstr);
							} catch (Exception e) {
								// TODO: handle exception
							}
							}
						}
					}
				}
			}
			if(bbs.getDate()==null){
				bbs.setDate(DateUtils.matchDate(doc.select("div.postBox-r div.postHead div.postTime").text()));
			}
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);

			String author=null;
			Elements select = doc.select("div.topic span.author a");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
			if(StringUtils.isBlank(author)){
				author = doc.select("div#forumcontainer div#imgTopic div.imgTopicCon div.imgTopicTit h4 span font").text();
			}
			bbs.setAuthor(author);
			/**
			 * 解析用于更新的地址
			 */
			bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null){
						Integer clickNum=0;
						Integer commentNum=0;
						Document doc = Jsoup.parse(html);
						String clickComentstr=doc.select("div.ctrlBox div.topPagesTwo form table tbody tr td").text();
						if(clickComentstr!=null && clickComentstr.trim().length()!=0){
							if(clickComentstr.indexOf("阅读:")!=-1){
								String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读:")+"阅读:".length());
								if(clickNumstr.indexOf(" ")!=-1){
									clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
									try {
										clickNum=Integer.parseInt(clickNumstr);
									} catch (Exception e) {
										// TODO: handle exception
									}
								}
							}
							if(clickComentstr.indexOf("参与:")!=-1){
								String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("参与:")+"参与:".length());
								if(commentNumstr.indexOf(" ")!=-1){
									commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
								try {
									commentNum=Integer.parseInt(commentNumstr);
								} catch (Exception e) {
									// TODO: handle exception
								}
								}
							}
						}else{
							clickComentstr=doc.select("div.imgTopic div.imgTopicCon div.imgTopicTit h4 span").text();
							if(clickComentstr!=null && clickComentstr.trim().length()!=0){
								if(clickComentstr.indexOf("阅读:")!=-1){
									String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读:")+"阅读:".length());
									if(clickNumstr.indexOf(" ")!=-1){
										clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
										try {
											clickNum=Integer.parseInt(clickNumstr);
										} catch (Exception e) {
											// TODO: handle exception
										}
									}
								}
								if(clickComentstr.indexOf("参与:")!=-1){
									String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("参与:")+"参与:".length());
									if(commentNumstr.indexOf(" ")!=-1){
										commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf(" "));
									try {
										commentNum=Integer.parseInt(commentNumstr);
									} catch (Exception e) {
										// TODO: handle exception
									}
									}
								}
							}else{
								clickComentstr=doc.select("div#container div.mainBodyLayout div.forumBody div.forumMain div#thread_mainBody div.ctrlBox div.adminBtn").text();
								if(clickComentstr!=null && clickComentstr.trim().length()!=0){
									if(clickComentstr.indexOf("阅读（")!=-1){
										String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读（")+"阅读（".length());
										if(clickNumstr.indexOf("）")!=-1){
											clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf("）"));
											try {
												clickNum=Integer.parseInt(clickNumstr);
											} catch (Exception e) {
												// TODO: handle exception
											}
										}
									}
									if(clickComentstr.indexOf("回复（")!=-1){
										String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复（")+"回复（".length());
										if(commentNumstr.indexOf("）")!=-1){
											commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf("）"));
										try {
											commentNum=Integer.parseInt(commentNumstr);
										} catch (Exception e) {
											// TODO: handle exception
										}
										}
									}
								}
							}
						}
						meta.setClickNum(clickNum);
						meta.setCommentNum(commentNum);
						return meta;
					}

				}
			}
		}
		return null;
	}
    
	
}
